package com.hc.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.domain.ProductCategory;
import com.hc.query.ProductCategoryCondition;
import com.hc.res.ProductCategoryItem;
import com.hc.service.ProductCategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author hecai
 */
@Slf4j
@RestController
@RequestMapping("/goodsCategory")
public class ProductCategoryController {

    @Resource
    private ProductCategoryService productCategoryService;

    @PostMapping("/v1")
    public Result<Null> create1(@RequestBody ProductCategory productCategory) {
        boolean res = productCategoryService.save(productCategory);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PostMapping("/v2")
    public Result<Null> create2(@RequestBody ProductCategory productCategory) {
        productCategory.setStatus(2);
        boolean res = productCategoryService.save(productCategory);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping("/v1")
    public Result<Null> updateById1(@RequestBody ProductCategory productCategory) {
        boolean res = productCategoryService.updateById(productCategory);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("/v1/{id}")
    public Result<Null> removeById1(@PathVariable Serializable id) {
        boolean res = productCategoryService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("/v1/{id}")
    public Result<ProductCategory> getById1(@PathVariable Serializable id) {
        ProductCategory res = productCategoryService.getById(id);

        return ResultUtil.<ProductCategory>success("查询成功").setData(res);
    }

    @ApiOperation(value = "查询所有的顶级类别", notes = "", httpMethod = "GET")
    @GetMapping("/v1")
    public Result<List<ProductCategoryItem>> getTopCategory() {
        ProductCategoryCondition condition = ProductCategoryCondition.builder()
                .pid(null)
                .build();
        List<ProductCategoryItem> list = productCategoryService.queryByCondition(condition).getRecords();
        return ResultUtil.<List<ProductCategoryItem>>success("查询成功").setData(list);
    }

    @ApiOperation(value = "根据条件查询：查询一级类别，查询一级类别下的子类别", notes = "", httpMethod = "GET")
    @GetMapping("/v1/pid/{pid}")
    public Result<List<ProductCategoryItem>> getSubCategories(@PathVariable("pid")Integer pid) {
        ProductCategoryCondition condition = ProductCategoryCondition.builder()
                .pid(pid)
                .build();
        List<ProductCategoryItem> list = productCategoryService.queryByCondition(condition).getRecords();
        return ResultUtil.<List<ProductCategoryItem>>success("查询成功").setData(list);
    }
}